﻿using a火.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lession2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Icon = Resources.logo_24;
            notifyIcon.Icon = Resources.logo_24;
        }

        private ContextMenu notifyiconMnu;
        
        /// <summary>
        /// 最小化到任务栏
        /// </summary>
        private void Initializenotifyicon()
        {
            //定义一个MenuItem数组，并把此数组同时赋值给ContextMenu对象 
            MenuItem[] mnuItms = new MenuItem[3];
            mnuItms[0] = new MenuItem();
            mnuItms[0].Text = "显示窗口";
            mnuItms[0].Click += new System.EventHandler(this.notifyIcon1_showfrom);
            mnuItms[1] = new MenuItem("-");
            mnuItms[2] = new MenuItem();
            mnuItms[2].Text = "退出系统";
            mnuItms[2].Click += new System.EventHandler(this.ExitSelect);
            mnuItms[2].DefaultItem = true;
            notifyiconMnu = new ContextMenu(mnuItms);
            notifyIcon.ContextMenu = notifyiconMnu;
            //为托盘程序加入设定好的ContextMenu对象 
        }

        public void notifyIcon1_showfrom(object sender, System.EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
                notifyIcon.Visible = true;
            }
        }

        public void ExitSelect(object sender, System.EventArgs e)
        {
            //隐藏托盘程序中的图标 
            notifyIcon.Visible = false;
            //关闭系统 
            this.Close();
            this.Dispose(true);
        }

        private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = true; //显示在系统任务栏
                this.WindowState = FormWindowState.Normal; //还原窗体
                notifyIcon.Visible = true; //托盘图标隐藏
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized) //判断是否最小化
            {
                this.ShowInTaskbar = true; //不显示在系统任务栏
                notifyIcon.Visible = true; //托盘图标可见
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Initializenotifyicon();
            notifyIcon.Visible = true;
        }

        private void btn_flash_Click(object sender, EventArgs e)
        {
            blinkTiemr.Enabled = true;
        }

        private Icon blank = Resources.blank;
        private Icon striped = Resources.logo;
        private bool blink = false;
        private void blinkTiemr_Tick(object sender, EventArgs e)
        {
            if (!blink)
            {
                this.notifyIcon.Icon = striped;
            }
            else
            {
                this.notifyIcon.Icon = blank;
            }
            blink = !blink;
        }

        private void stop_flash_Click(object sender, EventArgs e)
        {
            blinkTiemr.Enabled = false;
            this.notifyIcon.Icon = striped;
        }

        private void notifyIcon_MouseMove(object sender, MouseEventArgs e)
        {
            if (blinkTiemr.Enabled) {
                MessageBox.Show("alibaba!");
            }
        }
    }
}
